package com.zj.web.controller;


import com.zj.util.SnowFlakeIdGenrator;
import com.zj.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/id")
@Log
@Tag(name = "id生成器")
public class idController {
    @Autowired
    private SnowFlakeIdGenrator snowFlakeIdGenrator;

    //生成一个id
    @GetMapping("/next")
    @Operation(summary = "生成下一个id",description = "生成下一个id,格式：16位时间戳+数据中心id+工作节点id+序列号")
    public ResponseEntity<ResponseResult> getNextId(){
//        try {
//            Thread.sleep(8000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        Long id = snowFlakeIdGenrator.nextId();
        log.info("id生成成功："+id);
        return ResponseEntity.ok(ResponseResult.ok("id生成成功").setdata(id));
    }

    //生成多个id
    @GetMapping("/batch")
    @Operation(summary = "生成多个id",description = "生成多个id,格式：")
    public ResponseEntity<ResponseResult> getNextIds( @RequestParam(name = "size",defaultValue = "10") int size){
        List<Long> ids = snowFlakeIdGenrator.nextIds(size);
        return ResponseEntity.ok(ResponseResult.builder().code(1).data(ids).build());
    }

    //解析时间
    @GetMapping("/parse/time")
    @Operation(summary = "根据id解析时间",description = "解析id,格式：\"timeStamp\":1752283999482,\"date\":时间")
    public ResponseEntity<ResponseResult> parseTime(@RequestParam("id") Long id){
        long ts=snowFlakeIdGenrator.parseTimeStamp(id);
        return ResponseEntity.ok(ResponseResult.builder().code(1)
                .data(new TimeResult(ts,new Date(ts))).build());
    }
    //解析数据中心id
    @GetMapping("/parse/DataCenterId")
    @Operation(summary = "解析数据中心id",description = "解析数据中心id,格式：16位时间戳+数据中心id+工作节点id+序列号")
    public ResponseEntity<ResponseResult> parseDataCenterId(@RequestParam("id") Long id){
        return ResponseEntity.ok(ResponseResult.builder().code(1).data(snowFlakeIdGenrator.parseDataCenterId(id)).build());
    }
    //解析工作节点id
    @GetMapping("/parse/WorkerId")
    @Operation(summary = "解析工作节点id",description = "解析工作节点id,格式：16位时间戳+数据中心id+工作节点id+序列号")
    public ResponseEntity<ResponseResult> parseWorkerId(@RequestParam("id") Long id){
        return ResponseEntity.ok(ResponseResult.builder().code(1).data(snowFlakeIdGenrator.parseWorkerId(id)).build());
    }

    /**
     * 内部类封装时间
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class TimeResult{
        private long timeStamp;
        private Date date;
    }
}
